/*
* Copyright (c)  2017, ueboot.com
* All rights reserved.
* 2017-10-12 14:37:58
*/
package com.ueboot.weishop.admin.controller.setting;

import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.setting.vo.*;
import com.ueboot.weishop.entity.setting.SpecOptionValueEntity;
import com.ueboot.weishop.entity.setting.SpecificationEntity;
import com.ueboot.weishop.service.setting.SpecOptionValueService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;


/**
 *  Created by yangkui  on 2017-10-12 14:37:58.
 *
 * @since 2.0 by autocode
 */
@Slf4j
@RestController
@RequestMapping(value = "/specOptionValue")
public class SpecOptionValueController {

    @Autowired
    private SpecOptionValueService specOptionValueService;


    @RequiresPermissions("specOptionValue:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<SpecOptionValueResp>> page(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC)
                                                     Pageable pageable, @RequestBody(required = false) SpecOptionValueFindReq req){
        Page<SpecOptionValueEntity> entities = specOptionValueService.findBySpecId(pageable,req.getSpecId());
        Page<SpecOptionValueResp> body = entities.map(entity -> {
            SpecOptionValueResp resp = new SpecOptionValueResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        });

        return new Response<>(body);
    }


    @RequiresPermissions("specOptionValue:save")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody SpecOptionValueReq req) {
        SpecOptionValueEntity entity = null;
        if (req.getId() == null) {
            entity = new SpecOptionValueEntity();
        } else {
            entity = specOptionValueService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);
        Assert.notNull(req.getSpecId());
        SpecificationEntity spec = new SpecificationEntity();
        spec.setId(req.getSpecId());
        entity.setSpec(spec);
        specOptionValueService.save(entity);
        return new Response<>();
    }

    @RequiresPermissions("specOptionValue:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        specOptionValueService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("specOptionValue:read")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Response<SpecOptionValueResp> get(@PathVariable Long id) {
        SpecOptionValueEntity entity = specOptionValueService.get(id);
        SpecOptionValueResp resp = new SpecOptionValueResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

}
